package cn.wang.service.cargo.export.Impl;

import cn.wang.dao.BaseDao;

import cn.wang.entity.cargo.Contract;
import cn.wang.entity.cargo.ContractProduct;
import cn.wang.entity.cargo.ExtCproduct;
import cn.wang.entity.cargo.export.Export;
import cn.wang.entity.cargo.export.ExportProduct;
import cn.wang.entity.cargo.export.ExtEproduct;
import cn.wang.service.cargo.ContractProductService;
import cn.wang.service.cargo.ContractService;
import cn.wang.service.cargo.ExtCproductService;
import cn.wang.service.cargo.export.ExportProductService;
import cn.wang.service.cargo.export.ExportService;
import cn.wang.utils.Pagination;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by john on 2017/11/1.
 */
@Service("exportService")
@Transactional
public class ExprotServiceImpl implements ExportService {
    @Autowired
    private BaseDao<Export,String>baseDao;
    @Autowired
    private ContractProductService contractProductService;
    @Autowired
    private ExtCproductService extCproductService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private ExportProductService exportProductService;
    @Override
    public Pagination findPage(int pageNo, int pageSize, int i) {

        return baseDao.pageByHql("from Export where state=?",pageNo,pageSize,i);
    }

    @Override
    public List<Export> findAll() {
        return baseDao.getListByHQL("from Export");
    }

    @Override
    public void save(Export export) {
        //1修改合同状态已报运 2 根据合同id查询出商品
        String[] contractids=export.getId().split(", ");
        String contractNo="";
        for (String id:contractids) {
           Contract contract =contractService.findById(id);
            String cNo=contract.getContractNo();
            contractNo+=cNo+" ";
            contract.setState(2);//修改状态
            contractService.update(contract);
        }

        //2保存合同下的产品到报运商品表 保存商品下的附件到报运附件表中
        //2.1根据ids查询出合同下所有的产品集合
        List<ContractProduct>cplist=contractProductService.findByContractids(contractids);
        //export需要ContractProduct set集合 将list转为set
        Set<ExportProduct> epSet=new HashSet<>(cplist.size());
        Set<ExtEproduct>eeSet=new HashSet<>();
        for (ContractProduct cp:cplist) {
            ExportProduct ep=new ExportProduct();
            //设置ExportProduct关系
            ep.setExport(export);
            ep.setExPrice(cp.getPrice());
            ep.setCnumber(cp.getCnumber());
            ep.setFactory(cp.getFactory());
            ep.setProductNo(cp.getProductNo());

            //设置报运产品附件信息 查询出购销合同下产品附件设置给报运附件
            Set<ExtCproduct>etpSet=cp.getExtCproducts();
            for (ExtCproduct etp:etpSet) {
              ExtEproduct ee=  new ExtEproduct();
              //因为字段都相同 可以复制
                try {
                    BeanUtils.copyProperties(ee,etp);
                    //主键是自动生成的所以清空主键
                    ee.setId(null);
                    //关联关系设置了一方不处理关系 所以需要多方去维护关系
                    ee.setExportProduct(ep);
                } catch ( IllegalAccessException e ) {
                    e.printStackTrace();
                } catch ( InvocationTargetException e ) {
                    e.printStackTrace();
                }
                //将附件保存到集合中
                eeSet.add(ee);
            }
            //将报运附件集合设置到产品
            ep.setExtEproducts(eeSet);
            //将报运产品保存到集合中那个
            epSet.add(ep);
        }
        //将产品集合设置到报运单中export
        export.setExportProducts(epSet);
        //3设置报运合同编号为合同id,合同确认书
        //根据合同id查询出合同书 已空格的方式拼接
        String contractIds=export.getId().replaceAll(" ","");
       export.setContractIds(contractIds);
        export.setCustomerContract(contractNo);
        //制单日期
        export.setInputDate(new Date());
      baseDao.save(export);
    }

    @Override
    public void update(Export model) {
        baseDao.update(model);
    }

    @Override
    public void update(Export model, String[] mr_id, String[] mr_changed, Double[] mr_grossWeight,
                       Integer[] mr_orderNo, Integer[] mr_cnumber, Double[] mr_netWeight,
                       Double[] mr_sizeLength, Double[] mr_sizeWidth, Double[] mr_sizeHeight,
                       Double[] mr_exPrice, Double[] mr_tax) {

        //更新货物信息
        baseDao.update(model);

        for (int i=0;i<mr_id.length;i++){
            if("1".equals(mr_changed[i])){
               ExportProduct ep= exportProductService.findById(mr_id[i]);
                ep.setGrossWeight(mr_grossWeight[i]);
                ep.setOrderNo(mr_orderNo[i]);
                ep.setCnumber(mr_cnumber[i]);
                ep.setNetWeight(mr_netWeight[i]);
                ep.setSizeLength(mr_sizeLength[i]);
                ep.setSizeWidth(mr_sizeWidth[i]);
                ep.setSizeHeight(mr_sizeHeight[i]);
                ep.setExPrice(mr_exPrice[i]);
                ep.setTax(mr_tax[i]);
                exportProductService.update(ep);
            }
        }

    }

    @Override
    public Export findById(String id) {
        return baseDao.get(Export.class,id);
    }

    @Override
    public void delete(String[] ids) {
        //业务层放到service 保证在同一事务
        for (String id:ids) {
            baseDao.deleteById(Export.class,id);
        }
    }
}
